import {Injectable} from '@angular/core';
import {HttpInterceptor, HttpRequest, HttpHandler, HttpEvent} from '@angular/common/http';
import {Observable} from 'rxjs';

@Injectable()
export class JwtInterceptor implements HttpInterceptor {
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const excludeUrls = ['http://localhost:8080/chklogin/roleinfo','http://localhost:8080/login/myLogin'];
    if (excludeUrls.includes(request.url)) {
      // 如果是排除的URL，则直接传递请求，不添加JWT
      return next.handle(request);
    } else {
      const token = localStorage.getItem('jwt'); // 假设你将JWT存储在localStorage中
      if (token) {
        request = request.clone({
          setHeaders: {
            Authorization: `Bearer ${token}`
          }
        });
      }else{
        // 如果没有JWT的Token，也可以根据需要处理，比如重定向到登录页等
        return next.handle(request);
      }
    }
    return next.handle(request);
  }
}
